﻿using DailyApp.WPF.DTOs;
using DailyApp.WPF.HttpClients;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace DailyApp.WPF.ViewModels
{
    /// <summary>
    /// 待办事项视图模型
    /// </summary>
    internal class WaitUCViewModel : BindableBase, INavigationAware
    {
        #region 待办事项数据
        private List<WaitInfoDTO> _WaitList;
        /// <summary>
        /// 待办事项数据
        /// </summary>
        public List<WaitInfoDTO> WaitList
        {
            get { return _WaitList; }
            set
            {
                _WaitList = value;
                RaisePropertyChanged();
            }
        }
        #endregion


        private readonly HttpRestClient httpRestClient;//请求api的客户端
        /// <summary>
        /// 构造函数
        /// </summary>
        public WaitUCViewModel(HttpRestClient _httpRestClient)
        {
            httpRestClient = _httpRestClient;

            ShowAddWaitCmm = new DelegateCommand(ShowAddWait);
            QueryWaitListCmm = new DelegateCommand(QueryWaitList);
        }

        #region 查询
        public string SearchWaitTitle { get; set; }//查询条件

        private int _SearchWaitIndex;

        public int SearchWaitIndex
        {
            get { return _SearchWaitIndex; }
            set
            {
                _SearchWaitIndex = value;
                RaisePropertyChanged();
            }
        }


        public DelegateCommand QueryWaitListCmm { get; set; }
        /// <summary>
        /// 查询待办事项数据
        /// </summary>
        private void QueryWaitList()
        {

            int? status = null;
            if (SearchWaitIndex == 1) status = 0;
            if (SearchWaitIndex == 2) status = 1;
            ApiRequest apiRequest = new ApiRequest();
            apiRequest.Method = RestSharp.Method.GET;
            apiRequest.Route = $"Wait/QueryWait?badge={SearchWaitTitle}&status={status}";
            ApiResponse apiResponse = httpRestClient.Execute(apiRequest);
            if (apiResponse.ResultCode == 1)
            {
                WaitList = JsonConvert.DeserializeObject<List<WaitInfoDTO>>(apiResponse.ResultData.ToString());
                Visibility = (WaitList.Count > 0) ? Visibility.Hidden : Visibility.Visible;
            }
            else
            {
                WaitList = new List<WaitInfoDTO>();
                MessageBox.Show(apiResponse.Msg);
            }

        }
        #endregion

        #region 是否显示列表
        private Visibility _Visibility;

        public Visibility Visibility
        {
            get { return _Visibility; }
            set
            {
                _Visibility = value;
                RaisePropertyChanged();
            }
        }
        #endregion

        #region 显示添加待办
        private bool _isShowAddWait;
        /// <summary>
        /// 是否显示
        /// </summary>
        public bool IsShowAddWait
        {
            get { return _isShowAddWait; }
            set
            {
                _isShowAddWait = value;
                RaisePropertyChanged();
            }
        }
        private void ShowAddWait()
        {
            IsShowAddWait = true;
        }

        /// <summary>
        /// 接收
        /// </summary>
        /// <param name="navigationContext"></param>
        /// <exception cref="NotImplementedException"></exception>
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            if (navigationContext.Parameters.ContainsKey("SelectedIndex"))
            {
                SearchWaitIndex = navigationContext.Parameters.GetValue<int>("SelectedIndex");
            }
            else
            {
                SearchWaitIndex = 0;
            }
            QueryWaitList();
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
        }

        public DelegateCommand ShowAddWaitCmm { get; set; }//显示添加待办命令

        #endregion


    }
}
